﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAOLayer;
using MySql.Data;

namespace ServiceLayer
{
    public partial class FormTansportType : Form
    {
        CDAOTransportType TransportType = null;

        /// <summary>
        /// Конструктор, создаёт форму для редактирования списка типов транспорта в БД
        /// </summary>
        /// <param name="DataBase">База данных</param>
        public FormTansportType(CDataBase DataBase)
        {
            TransportType = new CDAOTransportType(DataBase);
            InitializeComponent();
        }

        private void UpdateData()
        {
            TransportType.Update();
            dgvTransportType.Rows.Clear();
            foreach (DataRow Row in TransportType.Table.Rows)
                dgvTransportType.Rows.Add(Row["transport_type_id"], Row["transport_type"]);
        }

        private void bOK_Click(object sender, EventArgs e)
        {
            //TransportType.Table.Clear();
            //foreach (DataGridViewRow Row in dgvTransportType.Rows)
            //    TransportType.Table.Rows.Add(Row.Cells["ID"].Value, 
            //                                 Row.Cells["TypeValue"].Value);
            Close();
        }


        private void bUpdate_Click(object sender, EventArgs e)
        {
            UpdateData();
        }

        private void FormTansportType_Load(object sender, EventArgs e)
        {
            UpdateData();
        }

        private void bCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dgvTransportType_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            
        }

        private void dgvTransportType_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (TransportType.UpdatingData) return;
            DataRow NewRow = TransportType.Table.NewRow();
            dgvTransportType.Rows[e.RowIndex].Cells["ID"].Value = NewRow["transport_type_id"].ToString();
            dgvTransportType.Rows[e.RowIndex].Cells["TypeValue"].Value = NewRow["transport_type"].ToString();
        }

        private void dgvTransportType_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {

        }

        private void dgvTransportType_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show("Added");
        }

        private void dgvTransportType_AllowUserToAddRowsChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Added");
        }
    }
}
